<template>
  <el-dialog
    :title="title"
    :width="width"
    :visible="visible"
    @close="visible = false"
  >
    <div class="code">
      <vue-qr ref="qrCode" :text="qrString" :size="190" :margin="10" />
      <div class="text-box">{{ this.qrName }}</div>
    </div>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="downloadQR">下载</el-button>
    </div>
  </el-dialog>
</template>
<script>
import vueQr from 'vue-qr'
export default {
  name: 'QrCode',
  components: { vueQr },
  props: {
    title: {
      type: String,
      default: '二维码'
    },
    width: {
      type: String,
      default: '26%'
    }
  },
  data() {
    return {
      qrString: '',
      qrName: '',
      visible: false
    }
  },
  methods: {
    open(qrName, qrString) {
      this.visible = true
      this.qrString = qrString
      this.qrName = qrName
    },
    // 下载事件处理函数
    downloadQR() {
      const a = document.createElement('a') // 下载的文件名
      a.download = '二维码' // url
      a.href = this.$refs.qrCode.$el.src // 触发点击
      a.click()
    }
  }
}
</script>
<style lang="scss" scoped>
.code {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
</style>
